load("//tools/lint:lint.bzl", "add_lint_tests")
load(
    "//tools/skylark:drake_py.bzl",
    "drake_py_binary",
    "drake_py_library",
    "drake_py_unittest",
)
load("//tools/skylark:drake_sh.bzl", "drake_sh_test")
load("//tools/skylark:sh.bzl", "sh_binary")

package(default_visibility = ["//visibility:public"])

drake_py_library(
    name = "module_py",
    srcs = ["__init__.py"],
    deps = ["//tools:module_py"],
)

drake_py_library(
    name = "util",
    srcs = ["util.py"],
    data = ["//:.bazelproject"],
    visibility = ["//visibility:private"],
    deps = [
        ":module_py",
        "@rules_python//python/runfiles",
    ],
)

drake_py_binary(
    name = "buildifier",
    srcs = ["buildifier.py"],
    data = [
        ":buildifier-tables.json",
        "@buildifier_prebuilt//:buildifier",
    ],
    deps = [
        ":module_py",
        ":util",
        "@rules_python//python/runfiles",
    ],
)

drake_py_library(
    name = "clang_format",
    testonly = 1,
    srcs = ["clang_format.py"],
    data = [
        "//:.clang-format",
        "@llvm//:bin/clang-format",
    ],
    deps = [
        ":module_py",
        "@rules_python//python/runfiles",
    ],
)

drake_py_binary(
    name = "clang-format",
    testonly = 1,
    srcs = ["clang_format.py"],
    deps = [
        ":clang_format",
    ],
)

drake_py_binary(
    name = "clang_format_lint",
    testonly = 1,
    srcs = ["clang_format_lint.py"],
    deps = [
        ":clang_format",
        ":module_py",
    ],
)

drake_py_binary(
    name = "drakelint",
    testonly = 1,
    srcs = ["drakelint.py"],
    deps = [
        ":module_py",
    ],
)

drake_py_binary(
    name = "install_lint_reporter",
    testonly = 1,
    srcs = ["install_lint_reporter.py"],
    deps = [
        ":module_py",
    ],
)

drake_py_binary(
    name = "library_lint_reporter",
    testonly = 1,
    srcs = ["library_lint_reporter.py"],
    deps = [
        ":module_py",
    ],
)

sh_binary(
    name = "ruff",
    srcs = ["ruff.sh"],
    data = [
        "@ruff",
    ],
)

exports_files(["ruff_format_lint.sh"])

# === test ===

drake_py_unittest(
    name = "buildifier_test",
    data = [":buildifier"],
)

drake_py_unittest(
    name = "util_test",
    tags = [
        # This test's pass/fail result depends on the presence of files in the
        # source tree beyond what is declared in its `deps`; thus, we need to
        # re-run the test anytime the source tree might have changed, for which
        # "external" is the closest semantic match.
        "external",
        # This test looks outside the sandbox.
        "no-sandbox",
        # Our custom install of kcov for Ubuntu 24.04 Noble in
        # setup/ubuntu/install_kcov.sh trips up this test.
        "no_kcov",
    ],
    deps = [":util"],
)

add_lint_tests()
